import Vue from "vue";
import App from "./App.vue";
import "./registerServiceWorker";
import router from "./router";
import store from "./store";
import { Loading } from "element-ui";

Vue.config.productionTip = false;

//全局样式
import "@/assets/css/global.css";

//应用element-ui
import "@/plugins/element";

//引用iconfont样式
import "@/assets/iconfont/iconfont.css";

//引入axios
import axios from "axios";

Vue.prototype.$http = axios;

let loadingInstance;
//请求拦截器
axios.interceptors.request.use(
  (config) => {
    //显示loading
    loadingInstance = Loading.service({
      fullscreen: true,
      text: "努力加载中...",
      background: "rgba(0,0,0,0.7)",
    });

    let token = sessionStorage.getItem("token");
    config.headers.Authorization = token;
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

//响应拦截器
axios.interceptors.response.use(
  (response) => {
    //关闭loading
    loadingInstance.close();
    //服务器返回数据
    return response;
  },
  function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
